package com.edu.exam.entity.examination.paper.technical;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import com.edu.exam.enums.examination.paper.ExceptionCategory;
import com.edu.exam.enums.examination.paper.ExceptionType;
import com.edu.exam.enums.common.SeverityLevel;

import jakarta.persistence.*;
import java.time.LocalDateTime;

/**
 * 处理异常实体类
 *
 * 根据E-04-02图像处理子模块数据库设计实现
 * 存储图像处理过程中的异常信息。
 *
 * @author System
 * @version 1.0.0
 */

@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class ProcessingException {

    /**
     * 异常ID，主键
     */
    @Id
    @Column(name = "exception_id", length = 64)
    private String exceptionId;

    /**
     * 处理记录ID
     */
    @Column(name = "processing_record_id", length = 64, nullable = false)
    private String processingRecordId;

    /**
     * 任务ID
     */
    @Column(name = "task_id", length = 64, nullable = false)
    private String taskId;

    /**
     * 图像ID
     */
    @Column(name = "image_id", length = 64)
    private String imageId;

    // 异常分类
    /**
     * 异常类别
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "exception_category", length = 30, nullable = false)
    private ExceptionCategory exceptionCategory;

    /**
     * 异常类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "exception_type", length = 50, nullable = false)
    private ExceptionType exceptionType;

    /**
     * 严重程度
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "severity_level", length = 20, nullable = false)
    private SeverityLevel severityLevel;

    // 异常信息
    /**
     * 错误代码
     */
    @Column(name = "error_code", length = 50, nullable = false)
    private String errorCode;

    /**
     * 错误消息
     */
    @Column(name = "error_message", columnDefinition = "TEXT", nullable = false)
    private String errorMessage;

    /**
     * 错误详情
     */
    @Column(name = "error_details", columnDefinition = "TEXT")
    private String errorDetails;

    /**
     * 堆栈跟踪
     */
    @Column(name = "stack_trace", columnDefinition = "TEXT")
    private String stackTrace;

    // 上下文信息
    /**
     * 处理阶段
     */
    @Column(name = "processing_stage", length = 30)
    private String processingStage;

    /**
     * 输入参数
     */
    @Column(name = "input_parameters", columnDefinition = "TEXT")
    private String inputParameters;

    /**
     * 环境信息
     */
    @Column(name = "environment_info", columnDefinition = "TEXT")
    private String environmentInfo;

    // 恢复信息
    /**
     * 恢复动作
     */
    @Column(name = "recovery_action", length = 100)
    private String recoveryAction;

    /**
     * 恢复是否成功
     */
    @Column(name = "recovery_successful")
    private Boolean recoverySuccessful;

    /**
     * 重试次数
     */
    @Column(name = "retry_count")
    private Integer retryCount;

    // 时间信息
    /**
     * 异常时间
     */
    @Column(name = "exception_time", nullable = false)
    private LocalDateTime exceptionTime;

    /**
     * 解决时间
     */
    @Column(name = "resolution_time")
    private LocalDateTime resolutionTime;

    /**
     * 解决耗时（毫秒）
     */
    @Column(name = "resolution_duration_ms")
    private Long resolutionDurationMs;

    /**
     * 解决人
     */
    @Column(name = "resolved_by", length = 64)
    private String resolvedBy;

    /**
     * 解决方法
     */
    @Column(name = "resolution_method", length = 100)
    private String resolutionMethod;

    /**
     * 预防措施
     */
    @Column(name = "prevention_measures", columnDefinition = "TEXT")
    private String preventionMeasures;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time")
    private LocalDateTime updatedTime;

    /**
     * 版本号
     */
    @Column(name = "version")
    private Integer version;

    // 业务方法

    /**
     * 获取异常类别描述
     */
    public String getExceptionCategoryDescription() {
        return exceptionCategory != null ? exceptionCategory.getDescription() : null;
    }

    /**
     * 获取异常类型描述
     */
    public String getExceptionTypeDescription() {
        return exceptionType != null ? exceptionType.getDescription() : null;
    }

    /**
     * 获取严重程度描述
     */
    public String getSeverityLevelDescription() {
        return severityLevel != null ? severityLevel.getDescription() : null;
    }

    /**
     * 检查是否为系统异常
     */
}
